using System.Collections.Generic;
using System.Collections.Specialized;

namespace HooIzDat.Web.Models
{
    public partial class Player
    {
        public virtual int PlayerID
        {
            get;
            set;
        }
    
        public virtual string UserName
        {
            get;
            set;
        }
    
        public virtual ICollection<Game> Games
        {
            get
            {
                if (_games == null)
                {
                    var newCollection = new FixupCollection<Game>();
                    newCollection.CollectionChanged += FixupGames;
                    _games = newCollection;
                }
                return _games;
            }
            set
            {
                if (!ReferenceEquals(_games, value))
                {
                    var previousValue = _games as FixupCollection<Game>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupGames;
                    }
                    _games = value;
                    var newValue = value as FixupCollection<Game>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupGames;
                    }
                }
            }
        }
        private ICollection<Game> _games;
    
        public virtual ICollection<Game> Games1
        {
            get
            {
                if (_games1 == null)
                {
                    var newCollection = new FixupCollection<Game>();
                    newCollection.CollectionChanged += FixupGames1;
                    _games1 = newCollection;
                }
                return _games1;
            }
            set
            {
                if (!ReferenceEquals(_games1, value))
                {
                    var previousValue = _games1 as FixupCollection<Game>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupGames1;
                    }
                    _games1 = value;
                    var newValue = value as FixupCollection<Game>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupGames1;
                    }
                }
            }
        }
        private ICollection<Game> _games1;
    
        public virtual ICollection<Game> Games2
        {
            get
            {
                if (_games2 == null)
                {
                    var newCollection = new FixupCollection<Game>();
                    newCollection.CollectionChanged += FixupGames2;
                    _games2 = newCollection;
                }
                return _games2;
            }
            set
            {
                if (!ReferenceEquals(_games2, value))
                {
                    var previousValue = _games2 as FixupCollection<Game>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupGames2;
                    }
                    _games2 = value;
                    var newValue = value as FixupCollection<Game>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupGames2;
                    }
                }
            }
        }
        private ICollection<Game> _games2;
    
        public virtual ICollection<GameState> GameStates
        {
            get
            {
                if (_gameStates == null)
                {
                    var newCollection = new FixupCollection<GameState>();
                    newCollection.CollectionChanged += FixupGameStates;
                    _gameStates = newCollection;
                }
                return _gameStates;
            }
            set
            {
                if (!ReferenceEquals(_gameStates, value))
                {
                    var previousValue = _gameStates as FixupCollection<GameState>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupGameStates;
                    }
                    _gameStates = value;
                    var newValue = value as FixupCollection<GameState>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupGameStates;
                    }
                }
            }
        }
        private ICollection<GameState> _gameStates;
    
        public virtual ICollection<GameTile> GameTiles
        {
            get
            {
                if (_gameTiles == null)
                {
                    var newCollection = new FixupCollection<GameTile>();
                    newCollection.CollectionChanged += FixupGameTiles;
                    _gameTiles = newCollection;
                }
                return _gameTiles;
            }
            set
            {
                if (!ReferenceEquals(_gameTiles, value))
                {
                    var previousValue = _gameTiles as FixupCollection<GameTile>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupGameTiles;
                    }
                    _gameTiles = value;
                    var newValue = value as FixupCollection<GameTile>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupGameTiles;
                    }
                }
            }
        }
        private ICollection<GameTile> _gameTiles;

        private void FixupGames(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (Game item in e.NewItems)
                {
                    item.Player1 = this;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (Game item in e.OldItems)
                {
                    if (ReferenceEquals(item.Player1, this))
                    {
                        item.Player1 = null;
                    }
                }
            }
        }
    
        private void FixupGames1(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (Game item in e.NewItems)
                {
                    item.Player2 = this;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (Game item in e.OldItems)
                {
                    if (ReferenceEquals(item.Player2, this))
                    {
                        item.Player2 = null;
                    }
                }
            }
        }
    
        private void FixupGames2(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (Game item in e.NewItems)
                {
                    item.Winner = this;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (Game item in e.OldItems)
                {
                    if (ReferenceEquals(item.Winner, this))
                    {
                        item.Winner = null;
                    }
                }
            }
        }
    
        private void FixupGameStates(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (GameState item in e.NewItems)
                {
                    item.ActivePlayer = this;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (GameState item in e.OldItems)
                {
                    if (ReferenceEquals(item.ActivePlayer, this))
                    {
                        item.ActivePlayer = null;
                    }
                }
            }
        }
    
        private void FixupGameTiles(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (GameTile item in e.NewItems)
                {
                    item.Player = this;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (GameTile item in e.OldItems)
                {
                    if (ReferenceEquals(item.Player, this))
                    {
                        item.Player = null;
                    }
                }
            }
        }
    }
}
